package 复习.博客2.哈希;

import java.util.HashMap;

public class 两数之和 {
    public int[] twoSum (int[] numbers, int target) {
        // write code here
        HashMap<Integer,Integer> res = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            if(!res.containsKey(target - numbers[i])){
                res.put(numbers[i],i);
            }else{
                return new int[]{res.get(target - numbers[i]) + 1,i + 1};
            }
        }
        return new int[2];
    }
}

/*
public int[] twoSum (int[] numbers, int target) {
        // write code here
        HashMap<Integer,Integer> map = new HashMap<>();
        map.put(numbers[0],0);
        for (int i = 1; i < numbers.length; i++) {
            int key = numbers[i];
            if(map.containsKey(target - key)){
                return new int[]{map.get(target - key) + 1,i + 1};
            }else{
                map.put(numbers[i],i);
            }
        }
        return null;
    }
 */